#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "MersenneTwister.h"
#include "ScriptMgr.h"
#include "Threading/Mutex.h"
#include "Util.h"
#include "Message.h"
#include "Effects.h"
#include "WorldPacket.h"
#include "Character.h"
#include "CharManager.h"
#include "Regions.h"
#include "RegionManager.h"
#include "WSSocket.h"
#include "GameDefine.h"
#include LOGIC_MGR_PATH
#include LOG_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include "AIScript.h"
#include "GameDataEnum.h"
#include "ProtocolDealEnums.h"
#include "ProtocolsManager.h"
#include "ResourceEventEnum.h"
#include XMLDATA_MGR_PATH
#include "AIInterface.h"
#include "AIScript.h"
#include "GameThreadLogic.h"
#include "VipCardManager.h"
#include "CenterBankManager.h"
#include "Tools.h"
#include "json/json.h"

namespace AIScript {
	AIChannelBaseInterface::AIChannelBaseInterface(Resource * pUnit):AIInterface(pUnit)
	{
		// m_AIType = AITYPE_TOWN;
	}
	
	bool AIChannelBaseInterface::Pause()							// 暂停游戏
	{
		m_bPause = true;
		Log.Debug("AIChannelBaseInterface::Pause","channel[%u]", m_Unit->getHandle());
		return true;
	}
	
	bool AIChannelBaseInterface::CancelPause()						// 取消暂停
	{
		m_bPause = false;
		Log.Debug("AIChannelBaseInterface::CancelPause","channel[%u]", m_Unit->getHandle());
		return true;
	}
	
	bool AIChannelBaseInterface::KickPlayer(const uint32 char_id)		// 踢出某个玩家
	{
		return true;
	}
	
	uint32 AIChannelBaseInterface::GetMaxPlayerNum()
	{
		return 0;
	}
	
	bool AIChannelBaseInterface::JoinChannel(CharPtr & pChr, bool bSendJoinTown)			//玩家加入牌桌
	{
		return true;
	}
	
	//-----------------------------------------------------------------------
	AITownBaseInterface::AITownBaseInterface(Resource * pUnit):AIInterface(pUnit)
	{
		// m_AIType = AITYPE_TOWN;
	}

	AITownBaseInterface::~AITownBaseInterface(void)
	{
		
	}
	
	//-----------------------------------------------------------------------
	void 	AITownBaseInterface::Load(void)
	{
		AIInterface::Load();
	}
	
	//-----------------------------------------------------------------------
	void 	AITownBaseInterface::LoadXmlDate(void)
	{
		m_nRoundLower = m_Unit->getUInt32Field("town_att");
		m_nRoundUpper = m_Unit->getUInt32Field("town_def");		
		m_need_lv = m_Unit->getUInt32Field("need_lv");
		m_gamePlayer = m_Unit->getUInt32Field("palmy");
		
		String xml = m_Unit->getStringField("user_data");
		if(xml != "")
		{
			m_xmlData.Load(xml);
		}	
	}
	
	//-----------------------------------------------------------------------
	bool 	AITownBaseInterface::JoinTown(CharPtr & pChr)
	{
		if(pChr.isNull())
			return false;
		
		WSSocketPtr socket = pChr->getSocket();	
		if(!pChr->isNPC() && socket.isNull())
			return false;		
		if (!CheckTownNeed(pChr))
			return false;
		AICreatureBaseInterface * pCBI = TO_CREATURE_BASE_INTERFACE(pChr->getAIInterface());		
		TownPtr oldTown = pCBI->GetTown();
		if (pChr->getStatus() != CharacterStatusFree)
		{			
			if (!oldTown.isNull())
			{
				if (oldTown->getHandle() == m_Unit->getHandle())
					return true;
			}
			Log.Error("AITownBaseInterface Test", "town[%u] pChr[%u] status[%u]", m_Unit->getHandle(), pChr->getHandle(), pChr->getStatus());
			return false;
		}
		WorldPacket newP;
		if (!socket.isNull() && sProtocolsMgr.CreateEnterTownPacket(&newP, m_Unit->getHandle(), en_ETS_Ok))
			socket->SendPacket(&newP);	

		if(!oldTown.isNull() && oldTown->getHandle() != m_Unit->getHandle())
		{
			return false;
		} 
		
		TownAIScript * ai = TO_TOWN_AI(m_Unit->getAIScript());
		if(ai == NULL || ai->OnJoinTown(pChr))
		{
			if(oldTown.isNull())
			{
				++ m_playernum;
				m_Unit->setUInt32Field("num", m_playernum.GetVal());
			}
			
			pCBI->SetTown(m_Unit);				
			return true;
		}
		
		Log.Debug("AITownBaseInterface Test", "pChr[%u] JoinTown[%u]   Now[%u], failure!!", 
					pChr->getHandle(), m_Unit->getHandle(), now());

		return false;
	}

	//-----------------------------------------------------------------------
	bool	AITownBaseInterface::LeaveTown(CharPtr & pChr)
	{
		if(pChr.isNull())
			return false;	
		
		/// TODO
		AICreatureBaseInterface * pCBI = TO_CREATURE_BASE_INTERFACE(pChr->getAIInterface());
		
		pCBI->SetTownNull();
		pCBI->SetChannelNull();
		
		if(m_playernum.GetVal())
			-- m_playernum;
		m_Unit->setUInt32Field("num", m_playernum.GetVal());
		
		pChr->setStatus(CharacterStatusFree);
		pChr->SaveDB();
		return true;
	}

	//-----------------------------------------------------------------------
	bool 	AITownBaseInterface::GameReady(CharPtr & pChr)
	{
		return true;
	}

	//-----------------------------------------------------------------------
	bool 	AITownBaseInterface::CheckTownNeed(CharPtr & pChr)
	{
		//游戏币和等级需求
		uint32 myCoins = sGLMgr.GetItemNum(pChr, ItemModel_Coins);
		WSSocketPtr pSocket = pChr->getSocket();
		WorldPacket packet;
		if (myCoins < m_Unit->getUInt32Field("coins"))
		{
			if (!pSocket.isNull() && sProtocolsMgr.CreateEnterTownPacket(&packet, m_Unit->getHandle(), en_ETS_BeHardUp))
				pSocket->SendPacket(&packet);	
			return false;
		}
		else if (pChr->getUInt32Field("lv") < m_Unit->getUInt32Field("need_lv"))
		{
			if (!pSocket.isNull() && sProtocolsMgr.CreateEnterTownPacket(&packet, m_Unit->getHandle(), en_ETS_Level))
				pSocket->SendPacket(&packet);	
			return false;
		}
		
		return true;
	}	
	
	//-----------------------------------------------------------------------
	void 	AITownBaseInterface::BroadcastPacket(WorldPacket * newP)
	{
		sGLMgr.BroadcastByTownID(newP, m_Unit->getHandle());	
	}
	
	//-----------------------------------------------------------------------
	uint32 	AITownBaseInterface::GetXMLData(const String & szName)
	{
		Town* pTown = (Town*)m_Unit;		
		uint32 val = 0;
		pTown->user_data.Get<uint32>(szName, val);		
		return val;
	}
	
	//-----------------------------------------------------------------------
	void 	AITownBaseInterface::SetXMLData(const String & szName, const uint32 & val)
	{
		Town* pTown = (Town*)m_Unit;
		if(pTown->user_data.isPresented(szName))
		{
			pTown->user_data.Set<uint32>(szName, val);
		}
		else
		{
			pTown->user_data.New(szName, val);
		}
		
		m_Unit->setStringField("user_data", pTown->user_data.getXML());
	}
	
	//-----------------------------------------------------------------------	
	void 	AITownBaseInterface::addRoomValve(const uint8 & roomlv)
	{
		++m_room_valve0;
	}
	
	//-----------------------------------------------------------------------
	void 	AITownBaseInterface::reduceRoomValve(const uint8 & roomlv)
	{
		--m_room_valve0;
	}
	
	//-----------------------------------------------------------------------
	uint32  AITownBaseInterface::getRoomValve(const uint8 & roomlv)
	{
		return m_room_valve0.GetVal();
	}
	
	/******************************角色基类******************************/	
	AICreatureBaseInterface::AICreatureBaseInterface(Resource * pUnit):AIInterface(pUnit)
	{
		m_ErrorCount = 0;
		m_LockTime = 0;
		m_nVipRoomID = 0;
		
		m_LeErrorCount = 0;
		m_LeLockTime = 0;
		
		m_nWantSitPos = 255;
	}
	
	AICreatureBaseInterface::~AICreatureBaseInterface(){}
	
	//-----------------------------------------------------------------------
	void AICreatureBaseInterface::AddNewVIPInfo(VIPCard &vipCard)
	{
		CharPtr pChr = ResourceProxy(m_Unit).getResourcePtr();
		// 加锁
		WGS_CHR_VIP_INNER_LOCK
		sVipCardMgr.AddNewVIPInfo(pChr, vipCard);
	}
	//-----------------------------------------------------------------------
	void AICreatureBaseInterface::AddVipInfo(VIPCard &vipCard, const uint32 endTime)
	{
		CharPtr pChr = ResourceProxy(m_Unit).getResourcePtr();
		// 加锁
		WGS_CHR_VIP_INNER_LOCK
		sVipCardMgr.AddVipInfo(pChr, vipCard, endTime);
	}
	//-----------------------------------------------------------------------
	void AICreatureBaseInterface::RemoveVipInfo()
	{
		CharPtr pChr = ResourceProxy(m_Unit).getResourcePtr();
		// 加锁
		WGS_CHR_VIP_INNER_LOCK
		sVipCardMgr.RemoveVipInfo(pChr);
	}
	//-----------------------------------------------------------------------
	uint32 AICreatureBaseInterface::GetXMLData(const String & szName)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		uint32 val = 0;
		pChr->user_data.Get<uint32>(szName, val);
		return val;
	}
	
	//-----------------------------------------------------------------------
	void 	AICreatureBaseInterface::SetXMLData(const String & szName, const uint32 & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChar = (Character*)m_Unit;
		if(pChar->user_data.isPresented(szName))
		{
		
			pChar->user_data.Set<uint32>(szName, val);
		}
		else
		{
			pChar->user_data.New<uint32>(szName, val);
		}
		
		if(isSave)
			pChar->user_data.Save();
	}
	
	//-----------------------------------------------------------------------
	void 	AICreatureBaseInterface::AddXMLValue(const String & szName, const uint32 & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		uint32 old_val = 0;
		if (pChr->user_data.Get<uint32>(szName, old_val))
		{
			pChr->user_data.Set<uint32>(szName, old_val + val);
		}
		else
			pChr->user_data.New<uint32>(szName, val);
		
		if(isSave)
			pChr->user_data.Save();
	}
	
	//-----------------------------------------------------------------------
	bool 	AICreatureBaseInterface::ReduceXMLValue(const String & szName, const uint32 & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		uint32 old_val = 0;
		if (!pChr->user_data.Get<uint32>(szName, old_val))
			return false;
		
		uint32 dest_val = 0;
		if (old_val >= val)
			dest_val = old_val - val;
		
		pChr->user_data.Set<uint32>(szName, dest_val);	
		if (isSave)
			pChr->user_data.Save();
		return true;
	}
	
	// 获取String XML内容
	String AICreatureBaseInterface::GetStringXMLData(const String & szName)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		String val = "";
		pChr->user_data.Get<String>(szName, val);
		return val;
	}
	
	// 设置String XML内容,未SaveDB
	void AICreatureBaseInterface::SetStringXMLData(const String & szName, const String & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChar = (Character*)m_Unit;
		if(pChar->user_data.isPresented(szName))
		{
			pChar->user_data.Set<String>(szName, val);
		}
		else
		{
			pChar->user_data.New<String>(szName, val);
		}
		
		if(isSave)
			pChar->user_data.Save();
	}
	
	//-----------------------------------------------------------------------
	int AICreatureBaseInterface::GetIntXMLData(const String & szName)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		int val = 0;
		pChr->user_data.Get<int>(szName, val);
		return val;
	}
	
	//-----------------------------------------------------------------------
	void 	AICreatureBaseInterface::SetIntXMLData(const String & szName, const int & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChar = (Character*)m_Unit;
		if(pChar->user_data.isPresented(szName))
		{
			pChar->user_data.Set<int>(szName, val);
		}
		else
		{
			pChar->user_data.New<int>(szName, val);
		}
		
		if(isSave)
			pChar->user_data.Save();
	}
	
	//-----------------------------------------------------------------------
	void 	AICreatureBaseInterface::AddIntXMLValue(const String & szName, const int & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		int old_val = 0;
		if (pChr->user_data.Get<int>(szName, old_val))
		{
			pChr->user_data.Set<int>(szName, old_val + val);
		}
		else
			pChr->user_data.New<int>(szName, val);
		
		if(isSave)
			pChr->user_data.Save();
	}
	
	//-----------------------------------------------------------------------
	void 	AICreatureBaseInterface::ReduceIntXMLValue(const String & szName, const int & val, bool isSave)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		int old_val = 0;
		if (pChr->user_data.Get<int>(szName, old_val))
		{
			pChr->user_data.Set<int>(szName, old_val - val);
		}
		else
			pChr->user_data.New<int>(szName, val);
		
		if(isSave)
			pChr->user_data.Save();
	}
	
	//-----------------------------------------------------------------------
	bool 	AICreatureBaseInterface::addTitle(const uint32 & tid, uint32 finishTime)
	{
		if (finishTime == 0)
			finishTime = time(0);
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		String szTitles = "";
		if (pChr->user_data.Get<String>("TITLES", szTitles))
		{
			szTitles += StringConverter::toString(tid)+","+StringConverter::toString(finishTime)+"|";
			pChr->user_data.Set<String>("TITLES", szTitles);
		}
		else
		{
			szTitles = StringConverter::toString(tid)+","+StringConverter::toString(finishTime)+"|";
			pChr->user_data.New<String>("TITLES", szTitles);
		}
		return true;
	}
	
	//-----------------------------------------------------------------------
	uint32 	AICreatureBaseInterface::getTitlesList(std::list<TitleInfo> * plist)
	{
	WGS_CHR_VIP_INNER_LOCK
		Character* pChr = (Character*)m_Unit;
		String key_name = "TITLES";
		std::vector< String > vecTitle;
		String szTitles = "";
		if (pChr->user_data.Get<String>(key_name, szTitles))
			vecTitle = StringUtil::split(szTitles, "|");
		if(vecTitle.empty())
			return 0;
		
		std::vector<String>::iterator it, endit = vecTitle.end();
		for(it = vecTitle.begin(); it != endit; ++ it)
		{
			std::vector<String> str = StringUtil::split((*it), ",");
			if(!str.empty())
			{
				TitleInfo tit;
				tit.id = StringConverter::parseUnsignedInt(str[0]);
				tit.create_time = StringConverter::parseUnsignedInt(str[1]);
				plist->push_back(tit);
			}
		}
		
		return plist->size();
	}
	
	//-----------------------------------------------------------------------
	bool 	AICreatureBaseInterface::HaveTitle(const uint32 & tid)
	{
		std::list<TitleInfo> titles;
		getTitlesList(&titles);
		std::list<TitleInfo>::iterator it, endit = titles.end();
		for(it = titles.begin(); it != endit; ++ it)
		{
			if((*it).id == tid)
				return true;
		}
		
		return false;
	}
	
	//-----------------------------------------------------------------------
	bool AICreatureBaseInterface::AddCreatureBody(const uint32 & bid, uint32 endtime/*  = 0 */)
	{
		if (endtime == 0)
			endtime = time(0);
		bool isFind = false;
		std::list<BodyData> plist;
		GetCreatureBodyList(plist);
		std::list<BodyData>::iterator iter, enditer = plist.end();
		for (iter = plist.begin(); iter != enditer; ++ iter)
		{
			BodyData &body = *iter;
			if (body.id == bid)
			{
				body.endtime = endtime;
				isFind = true;
			}
		}		
		if (isFind == false)
		{
			BodyData body;
			body.id = bid;
			body.endtime = endtime;
			plist.push_back(body);
		}
		WriteCreatureBodyList(plist);
		return true;
	}
	//-----------------------------------------------------------------------
	void AICreatureBaseInterface::WriteCreatureBodyList(std::list<BodyData> &plist)
	{
		Character* pChr = (Character*)m_Unit;
		String key_name = "BODY_DATA";
		String szBody = "";
		std::list<BodyData>::iterator iter, enditer = plist.end();
		for (iter = plist.begin(); iter != enditer; ++ iter)
		{
			szBody += StringConverter::toString((*iter).id)+","+StringConverter::toString((*iter).endtime)+"|";			
		}
		pChr->user_data.New<String>(key_name, szBody);
		pChr->user_data.Save();
	}
	//-----------------------------------------------------------------------
	uint32 AICreatureBaseInterface::GetCreatureBodyList(std::list<BodyData> &plist)
	{
		Character* pChr = (Character*)m_Unit;
		String key_name = "BODY_DATA";
		std::vector< String > vcBody;
		{
			if(pChr->user_data.isPresented(key_name))
			{
				String szBody = "";
				if (pChr->user_data.Get<String>(key_name, szBody))
					vcBody = StringUtil::split(szBody, "|");
			}
		}		
		if(vcBody.empty())
			return 0;		
		uint32 t_time = time(0);
		std::vector<String>::iterator it, endit = vcBody.end();
		for(it = vcBody.begin(); it != endit; ++ it)
		{
			std::vector<String> str = StringUtil::split((*it), ",");
			if(!str.empty())
			{				
				BodyData body;
				body.id = StringConverter::parseUnsignedInt(str[0]);
				body.endtime = StringConverter::parseUnsignedInt(str[1]);
				if (body.endtime > t_time)
					plist.push_back(body);				
			}
		}		
		return plist.size();	
	}
	//-----------------------------------------------------------------------
	bool AICreatureBaseInterface::HaveCreatureBody(const uint32 & bid)
	{
		std::list<BodyData> lstBody;
		GetCreatureBodyList(lstBody);
		std::list<BodyData>::iterator it, endit = lstBody.end();
		for(it = lstBody.begin(); it != endit; ++ it)
		{
			if((*it).id == bid)
				return true;
		}
		
		return false;	
	}
	
	//-----------------------------------------------------------------------
	uint8 AICreatureBaseInterface::ValidateSecondPassword(const String & Pwd)
	{
		UserPtr pUser = sUserMgr.getByHandle(m_Unit->getUInt32Field("userid"));
		if (pUser.isNull())
			return en_Operator2PwdResult_OldPwdError;
		uint32 errorCount = sXmlDataMgr.GetConfXMLValue("2PWD_ERROR_COUNT");
		if (m_ErrorCount >= errorCount)
		{
			int32 leftTime = sXmlDataMgr.GetConfXMLValue("2PWD_LOCK_TIME") - (time(0) - m_LockTime) / 60;
			if (leftTime > 0)
			{
				sGLMgr.Pwd2LockedMessage(pUser, leftTime);
				return en_Operator2PwdResult_Locked;
			}
			else
			{
				m_ErrorCount -= m_ErrorCount;
				m_LockTime -= m_LockTime;
			}
		}
		
		String dbPwd = sBankMgr.GetPassworld(pUser);
		if (Pwd == dbPwd)
		{
			m_ErrorCount -= m_ErrorCount;
			m_LockTime -= m_LockTime;
			m_bValidated = true;
			return en_Operator2PwdResult_Success;
		}
		if (++m_ErrorCount == errorCount)
		{
			m_LockTime = time(0);
			sGLMgr.Pwd2LockedMessage(pUser, sXmlDataMgr.GetConfXMLValue("2PWD_LOCK_TIME"));
			return en_Operator2PwdResult_Locked;
		}
		
		return en_Operator2PwdResult_OldPwdError;
	}
	
	bool AICreatureBaseInterface::GainGift(const uint8 & type, const uint32 & data1)
	{
		return false;
	}
	
	//------------------------------------乐码相关代码Begin-------------------------------------------
	// 创建乐码并并发送到玩家邮件
	bool AICreatureBaseInterface::CreateLECardToChar()
	{
		UserPtr pUser = sUserMgr.load(m_Unit->getUInt32Field("userid"));
		if(pUser.isNull())
			return false;
		
		CharPtr pChr = sCharMgr.getByHandle(m_Unit->getHandle());
		if(pChr.isNull())
			return false;
		
		uint32 nowtime = time(0);
		std::list<GameExchange> cards,realCards;
		
		uint32 reg_days = sTools.GetOffsetDate(pUser->getUInt32Field("reg_time"), nowtime);
		std::vector<LeCardGive> leCards = sXmlDataMgr.getGiveLeCardsInfo(reg_days);
		if(leCards.empty())
			return false;
		
		//先清除之前的乐码
		uint32 pid = pUser->getUInt32Field("platform_id");
		if(pid)
		{
			uint32 count = sItemMgr.getExchangeDBCount("(source = %u)",pid);
			if(count)
			{
				std::list<GameExchange> exchanges;
				sItemMgr.getExchangeListDB(&exchanges,0,count,"(source = %u)",pid);
				
				std::list<GameExchange>::iterator iter,ei = exchanges.end();
				for(iter = exchanges.begin(); iter!=ei; ++iter)
				{
					if((*iter).data2 <= now())
					{
						sItemMgr.deleteExchangeDB((*iter).exchange_id);
					}
				}
			}
		}
		
		std::map<uint32,uint32> mapCardsType;
		
		std::vector<LeCardGive>::iterator iter,ei = leCards.end();
		for(iter = leCards.begin(); iter!=ei; ++iter)
		{
			mapCardsType[(*iter).type] += (*iter).num;
		}
		
		std::map<uint32,uint32>::iterator itMap,eiMap = mapCardsType.end();
		for(itMap = mapCardsType.begin(); itMap!=eiMap; ++itMap)
		{
			if(itMap->second)
			{
				sGLMgr.CreateLECards(&cards,pid,itMap->second,itMap->first);
			}
		}
		
		sBankMgr.SetPublicData(pUser, en_PublicData_GetLemaTime, time(0));
		
		WorldPacket packet;
		WSSocketPtr socket = pChr->getSocket();
		
		uint32 curr_num = sItemMgr.getExchangeDBCount("(source = %u and (status = %u or status = %u) )",pid,enExchangeStataus_Process,enExchangeStataus_YanZheng);
		if(curr_num)
		{
			sItemMgr.getExchangeListDB(&realCards,0,curr_num,"(source = %u and (status = %u or status = %u) )",pid,enExchangeStataus_Process,enExchangeStataus_YanZheng);
		}
		
		if(!socket.isNull() && sProtocolsMgr.CreateLeCardsListPacket(&packet,&realCards))
			socket->SendPacket(&packet);
		
		time_t getLemaTime = sBankMgr.GetPublicData(pUser, en_PublicData_GetLemaTime);
		tm getLeTm = *(localtime(&getLemaTime));
		uint8 distence_days = getLeTm.tm_wday == 0 ? 1 : 8 - getLeTm.tm_wday;
		
		if(!socket.isNull() && sProtocolsMgr.CreateLeCardsCoolDownPacket(&packet,distence_days))
			socket->SendPacket(&packet);
		
		return true;
	}
	
	//验证一张打折卡(卡状态)
	uint8 AICreatureBaseInterface::CheckLECard(String & checkNode)
	{
		CharPtr pChr = sCharMgr.getByHandle(m_Unit->getHandle());
		if(pChr.isNull())
			return en_CardCheck_None;
		
		if(checkNode == "")
			return en_CardCheck_NotExist;
		
		UserPtr pUser = sUserMgr.getByHandle(m_Unit->getUInt32Field("userid"));
		if (pUser.isNull())
			return en_CardCheck_None;
		
		uint32 errorCount = sXmlDataMgr.GetConfXMLValue("LEWD_ERROR_COUNT");
		
		if (m_LeErrorCount >= errorCount)
		{
			int32 leftTime = sXmlDataMgr.GetConfXMLValue("LEWD_LOCK_TIME") - (time(0) - m_LeLockTime) / 60;
			if (leftTime > 0)
			{
				sGLMgr.LewdLockedMessage(pUser, leftTime);
				return en_CardCheck_Lock;
			}
			else
			{
				m_LeErrorCount = 0;
				m_LeLockTime = 0;
			}
		}
		
		GameExchange gameEx;
		bool bFind = sItemMgr.getExchangeDB(checkNode,&gameEx);
		if(!bFind)
		{
			if (++m_LeErrorCount == errorCount)
			{
				m_LeLockTime = time(0);
				sGLMgr.LewdLockedMessage(pUser, sXmlDataMgr.GetConfXMLValue("LEWD_LOCK_TIME"));
				return en_CardCheck_Lock;
			}
			
			return en_CardCheck_NotExist;
		}
		
		uint8 status = gameEx.status;
		if(status == enExchangeStataus_YanZheng)
			return en_CardCheck_YanZheng;
		
		if(status == enExchangeStataus_Exchaned)
			return en_CardCheck_Used;
		
		if(status == enExchangeStataus_TimeOver)
			return en_CardCheck_TimeOver;
		
		uint32 pid = pUser->getUInt32Field("platform_id");
		
		WorldPacket packet;
		WSSocketPtr socket = pChr->getSocket();
		if(gameEx.data2 <= time(0))	//过期
		{
			gameEx.status = enExchangeStataus_TimeOver;
			sItemMgr.updateExchangeDB(&gameEx);
			
			if(!socket.isNull() && sProtocolsMgr.CreateLeCardsStatusPacket(&packet,gameEx.exchange_id,gameEx.status))
				socket->SendPacket(&packet);
			
			//过期日志
			sBankMgr.LeMaOpLog(pid,en_LMOM_TimeOver,gameEx.exchange_id);
			return en_CardCheck_TimeOver;
		}
		
		m_LeErrorCount -= m_LeErrorCount;
		m_LeLockTime -= m_LeLockTime;
		
		//检测状态
		char buf[256] = "";
		sprintf(buf," and (status = %u)",enExchangeStataus_Process);
		gameEx.source = pid;
		gameEx.status = enExchangeStataus_YanZheng;
		sItemMgr.updateExchangeDB(&gameEx,buf);
		
		GameExchange newExchange;
		sItemMgr.getExchangeDB(gameEx.exchange_id,&newExchange);
		if(newExchange.status == enExchangeStataus_YanZheng)
		{
			if(!socket.isNull() && sProtocolsMgr.CreateLeCardsStatusPacket(&packet,gameEx.exchange_id,gameEx.status))
				socket->SendPacket(&packet);
			
			//验证日志
			sBankMgr.LeMaOpLog(pid,en_LMOM_Check,gameEx.exchange_id);
			return en_CardCheck_CanUse;
		}
		
		return en_CardCheck_Used;
	}
	
	const uint32 AICreatureBaseInterface::GetFrozenCapital(const uint32 modelId)
	{
		WGS_CHR_FROZEN_INNER_LOCK
		return m_mapFrozen[modelId];
	}
	
	bool AICreatureBaseInterface::AddFrozenCapital(const uint32 modelId, const uint32 value)
	{
		WGS_CHR_FROZEN_INNER_LOCK
		uint32 &fv = m_mapFrozen[modelId];
		CharPtr pChr = ResourceProxy(m_Unit).getResourcePtr();
		if (sGLMgr.GetItemNum(pChr, modelId) < fv + value)
			return false;
		fv += value;
		return true;
	}
	
	void AICreatureBaseInterface::ReleaseFrozenCapital(const uint32 modelId, const uint32 value)
	{
		WGS_CHR_FROZEN_INNER_LOCK
		uint32 &fv = m_mapFrozen[modelId];
		if (value > 0 && fv > value)
			fv -= value;
		else
			fv = 0;
	}
	
	// 初始通用在线奖励
	bool AICreatureBaseInterface::InitCommonOnlineGiftData()
	{
		stCommonOnlineGift stCOG = sXmlDataMgr.GetCommonOnlineGift();
		uint32 now_time = time(0);
		
		{
		WGS_CHR_VIP_INNER_LOCK
			bool bFindData = false;
			String strGift = "";
			Character* pCtr = (Character*)m_Unit;
			
			if (pCtr->user_data.Get<String>(stCOG.strOnlineKey.c_str(), strGift))
			{
				sGLMgr.ChangeXianToYinHao(strGift);
				
				Json::Reader reader; 
				Json::Value val, val2, val3;
				reader.parse(strGift, val);
				if(val.size())
				{
					bFindData = true;
					for(Json::Value::iterator it=val.begin(); it!=val.end(); ++it)
					{
						// 同一天无需更新
						if(sTools.IsSameDate((*it)["update_time"].asUInt(), now_time))
							return false;
					}
					
					uint8 index = 0;
					std::map<uint32, stCommonSingleGift>::iterator itMap, eiMap = stCOG.mapGift.end();
					for(itMap = stCOG.mapGift.begin(); itMap!=eiMap; ++itMap)
					{
						val2["id"] = itMap->second.id;					// ID
						val2["status"] = enOnlineGiftStatus_Process;	// 计时中
						val2["need_seconds"] = itMap->second.seconds;	// 需要时间
						val2["curr_seconds"] = 0;						// 当前累计时间
						val2["coins"] = itMap->second.coins;			// 奖励铜钱
						val2["update_time"] = now_time;					// 更新时间
						val3[index++] = val2;									
					}
					
					Json::FastWriter w;
					strGift = w.write(val3);
					
					sGLMgr.ChangeYinHaoToXian(strGift);
					
					pCtr->user_data.Set<String>(stCOG.strOnlineKey.c_str(), strGift);
					pCtr->user_data.Save();
					m_Unit->SaveDB();
				}
			}
			
			if(!bFindData)
			{
				uint8 index = 0;
				Json::Value val, val2;
				std::map<uint32, stCommonSingleGift>::iterator itMap, eiMap = stCOG.mapGift.end();
				for(itMap = stCOG.mapGift.begin(); itMap!=eiMap; ++itMap)
				{
					val2["id"] = itMap->second.id;					// ID
					val2["status"] = enOnlineGiftStatus_Process;	// 计时中
					val2["need_seconds"] = itMap->second.seconds;	// 需要时间
					val2["curr_seconds"] = 0;						// 当前累计时间
					val2["coins"] = itMap->second.coins;			// 奖励铜钱
					val2["update_time"] = now_time;					// 更新时间
					val[index++] = val2;									
				}
				
				Log.Debug("AICreatureBaseInterface::InitCommonOnlineGiftData","strOnlineKey[%s] strGift[%s]", stCOG.strOnlineKey.c_str(), strGift.c_str());
				Json::FastWriter w;
				strGift = w.write(val);
				
				sGLMgr.ChangeYinHaoToXian(strGift);
				
				Log.Debug("InitCommonOnlineGiftData","strGift[%s]", strGift.c_str());
				
				pCtr->user_data.New<String>(stCOG.strOnlineKey.c_str(), strGift);
				pCtr->user_data.Save();
				m_Unit->SaveDB();
			}
		}
		
		// 数据初始完后发送列表信息
		SendCommonOnlineGift();
		return true;
	}
	
	// 更新通用在线列表数据
	void AICreatureBaseInterface::UpdateCommonOnlineGiftData(bool bOffline)
	{
		CharPtr pChr = sCharMgr.getByHandle(m_Unit->getHandle());
		if(pChr.isNull())
			return ;
		
		UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
		if(pUser.isNull())
			return ;
		
		uint32 now_time = time(0);
		uint32 ll_time = pUser->getUInt32Field("ll_time");
		if(!sTools.IsSameDate(ll_time, now_time))
			return ;
		
		uint32 online_seconds = now_time - ll_time;
		
		stCommonOnlineGift stCOG = sXmlDataMgr.GetCommonOnlineGift();
		
	WGS_CHR_VIP_INNER_LOCK
		
		String strGift = "";
		WorldPacket packet;
		Character* pCtr = (Character*)m_Unit;
		if(pCtr->user_data.Get<String>(stCOG.strOnlineKey.c_str(), strGift))
		{
			sGLMgr.ChangeXianToYinHao(strGift);
			
			Json::Reader reader; 
			Json::Value val;
			
			reader.parse(strGift, val);
			if(val.size())
			{
				bool bSave = false;
				for(Json::Value::iterator it=val.begin(); it!=val.end(); ++it)
				{
					// 状态已经发生变化
					if((*it)["status"].asUInt() != enOnlineGiftStatus_Process)
						continue;
					
					if(bOffline)
						bSave = true;
					
					uint32 need_seconds = (*it)["need_seconds"].asUInt();
					uint32 curr_seconds = (*it)["curr_seconds"].asUInt();
					Log.Debug("UpdateCommonOnlineGiftData","char[%u] curr_seconds[%u] need_seconds[%u] online_seconds[%u]", 
										m_Unit->getHandle(), curr_seconds, need_seconds, online_seconds);
					if(curr_seconds + online_seconds >= need_seconds)
					{
						(*it)["curr_seconds"] = need_seconds;			// 更新时间
						(*it)["status"] = enOnlineGiftStatus_CanGet;	// 可领取
						bSave = true;
					}
					else
					{
						if(bOffline)
						{
							(*it)["curr_seconds"] = curr_seconds + online_seconds;
						}
					}
				}
				
				if(bSave)
				{
					Json::FastWriter w;
					strGift = w.write(val);
					
					sGLMgr.ChangeYinHaoToXian(strGift);
					
					pCtr->user_data.Set<String>(stCOG.strOnlineKey.c_str(), strGift);
					pCtr->user_data.Save();
					m_Unit->SaveDB();
				}
			}
		}
		
	}
	
	// 发送通用在线奖励
	void AICreatureBaseInterface::SendCommonOnlineGift()
	{
		CharPtr pChr = sCharMgr.getByHandle(m_Unit->getHandle());
		if(pChr.isNull())
			return ;
		
		stCommonOnlineGift stCOG = sXmlDataMgr.GetCommonOnlineGift();
		
	WGS_CHR_VIP_INNER_LOCK
		
		String strGift = "";
		WorldPacket packet;
		Character* pCtr = (Character*)m_Unit;
		if (pCtr->user_data.Get<String>(stCOG.strOnlineKey.c_str(), strGift))
		{
			sGLMgr.ChangeXianToYinHao(strGift);
			
			if(sProtocolsMgr.CreateNoticeCommonOnlineGiftPacket(&packet, strGift))
				sGLMgr.SendProtocolsToChr(pChr, &packet);
		}
		else
		{
			if(sProtocolsMgr.CreateNoticeCommonOnlineGiftPacket(&packet, strGift))
				sGLMgr.SendProtocolsToChr(pChr, &packet);
		}
		
	}
	
	// 领取通用在线奖励
	bool AICreatureBaseInterface::GetCommonOnlineGiftBounty(const uint32 & id)
	{
		// 检查前先更新一下礼包数据
		UpdateCommonOnlineGiftData();
		
		stCommonOnlineGift stCOG = sXmlDataMgr.GetCommonOnlineGift();
		
		CharPtr pChr = sCharMgr.getByHandle(m_Unit->getHandle());
		if(pChr.isNull())
			return false;
		
	WGS_CHR_VIP_INNER_LOCK
		
		uint8 result = enGetOnlineGiftResult_Sucess;
		String strGift = "";
		WorldPacket packet;
		Character* pCtr = (Character*)m_Unit;
		if (pCtr->user_data.Get<String>(stCOG.strOnlineKey.c_str(), strGift))
		{
			sGLMgr.ChangeXianToYinHao(strGift);
			Json::Reader reader; 
			Json::Value val;
			reader.parse(strGift, val);
			if(val.size())
			{
				bool bExist = false;
				for(Json::Value::iterator it=val.begin(); it!=val.end(); ++it)
				{
					if((*it)["id"].asUInt() != id)
						continue;
					
					bExist = true;
					if((*it)["status"].asUInt() == enOnlineGiftStatus_Process)
					{// 时间未到
						result = enGetOnlineGiftResult_Time;
						break;
					}
					
					if((*it)["status"].asUInt() == enOnlineGiftStatus_Getted)
					{// 已领取
						result = enGetOnlineGiftResult_Getted;
						break;
					}
					
					// 修改状态
					(*it)["status"] = enOnlineGiftStatus_Getted;
					
					// Save数据
					Json::FastWriter w;
					strGift = w.write(val);
					
					sGLMgr.ChangeYinHaoToXian(strGift);
					pCtr->user_data.Set<String>(stCOG.strOnlineKey.c_str(), strGift);
					pCtr->user_data.Save();
					m_Unit->SaveDB();
					
					uint32 coins = (*it)["coins"].asUInt();
					sGLMgr.addItemNum(pChr, ItemModel_Coins, coins);
					sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_Hortation, ItemModel_Coins, en_UNT_Add, coins, en_HSI_Online, id);
					
					// 发送领取结果协议
					WorldPacket packet;
					if(sProtocolsMgr.CreateGetCommonOnlineGiftResultePacket(&packet, id, coins, result))
						sGLMgr.SendProtocolsToChr(pChr, &packet);
				}
				
				if(bExist)
				{
					result = enGetOnlineGiftResult_ErrorID;
					// 发送领取结果
					WorldPacket packet;
					if(sProtocolsMgr.CreateGetCommonOnlineGiftResultePacket(&packet, id, 0, result))
						sGLMgr.SendProtocolsToChr(pChr, &packet);
				}
			}
		}
		
		return true;
	}
	
//------------------------------------乐码相关代码End---------------------------------------------
	
	/******************************效果基类******************************/	
	AIEffectInterface::AIEffectInterface(Resource* pUnit):AIInterface(pUnit)
	{
		m_AIType = AITYPE_EFFECT;
		m_effectStatus = EffectStatus_None;
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::init(void)
	{
		m_interval_time	= 0;
		m_triggerMax 	= 0;
		m_currTriggerCount = 0;
		
		m_src_pos 	= Vector3::ZERO;
		m_dest_pos 	= Vector3::ZERO;
		m_congeal_time = 0;
		m_congeal_last_time = 0;
		m_exp_duration_time = 0;
		m_leave_duration_time = 0;	
		// m_isRemoved.SetVal(false);
		
		Effect* Unit = (Effect*)m_Unit;
		m_effectStatus = EffectStatus_None;
		m_ownerProxy = Unit->common.uplink;
		
		uint8 src_type = m_Unit->getUInt32Field("source_type");
		uint32 src_id	= m_Unit->getUInt32Field("source_id");		
		m_srcProxy._type = ResourceProxy::getResourceType((ResourceTargetType)src_type);
		m_srcProxy._handle = src_id;		
		
		uint8 tag_type = m_Unit->getUInt32Field("target_type");
		uint32 tag_id = m_Unit->getUInt32Field("target_id");
		m_tagProxy._type = ResourceProxy::getResourceType((ResourceTargetType)tag_type);
		m_tagProxy._handle = tag_id;		
		
		m_src_pos.x = m_Unit->getFloatField("src_x");
		m_src_pos.y = m_Unit->getFloatField("src_y");
		m_src_pos.z = m_Unit->getFloatField("src_z");
		
		m_dest_pos.x = m_Unit->getFloatField("dest_x");
		m_dest_pos.y = m_Unit->getFloatField("dest_y");
		m_dest_pos.z = m_Unit->getFloatField("dest_z");		
		
		
		m_interval_time = Unit->model->getUInt32Field("interval_time");
		m_duration_time = Unit->model->getUInt32Field("duration_time");
		
		m_triggerMax = m_Unit->getUInt32Field("loop");
		
		Unit->common.start_time = m_Unit->getUInt32Field("start_time");
		Unit->common.end_time = m_Unit->getUInt32Field("end_time");	
		Unit->common.expend_time = now();
		
		m_effectRangeType = (EffectRangeType)Unit->model->getUInt32Field("range_type");
		m_effectTargetType= Unit->model->getUInt32Field("target_type");
		
		if(m_effectRangeType == EffectRange_Round)
		{
			float range = float(Unit->model->getUInt32Field("range_x"));
			m_effectRect.fx = -range;
			m_effectRect.fy = -range;
			m_effectRect.fw = range;
			m_effectRect.fh = range;
		}
		else if(m_effectRangeType == EffectRange_Rectangle)
		{
			float range_x = float(Unit->model->getUInt32Field("range_x"));
			float range_y = float(Unit->model->getUInt32Field("range_y"));
			m_effectRect.fw = range_x;
			m_effectRect.fh = range_y;
		}
		
		Unit->common.data1 = m_Unit->getInt32Field("data1");
		Unit->common.data2 = m_Unit->getInt32Field("data2");
		Unit->common.data3 = m_Unit->getInt32Field("data3");
		Unit->common.data4 = m_Unit->getInt32Field("data4");
		
		Unit->common.fdata1= m_Unit->getFloatField("fdata1");
		Unit->common.fdata2= m_Unit->getFloatField("fdata2");
		Unit->common.fdata3= 0.0f;
		Unit->common.fdata4= 0.0f;
		
		m_effect1 = Unit->model->getUInt32Field("effect1");
		m_effect2 = Unit->model->getUInt32Field("effect2");
		m_effect3 = Unit->model->getUInt32Field("effect3");
		m_effect4 = Unit->model->getUInt32Field("effect4");
		m_effect5 = Unit->model->getUInt32Field("effect5");
		m_effect6 = Unit->model->getUInt32Field("effect6");
		m_effect7 = Unit->model->getUInt32Field("effect7");
		m_sub_effect1 = Unit->model->getUInt32Field("effect8");
		m_sub_effect2 = Unit->model->getUInt32Field("effect9");
		
		int32 leave = int32(Unit->common.end_time - time(0));
		if(leave > 0)
			m_leave_duration_time = uint32(leave * 1000);
		
		m_model_id = Unit->model->getHandle();
		m_start_time = now();		
		Unit->common.start_ms_time = Unit->common.start_time;
		Unit->common.end_ms_time = Unit->common.end_time;
		
		m_xmlData.Load(Unit->model->getStringField("effect_string"));
		
		ResourceEvent event;
		event.event_id = ResourceEvent_Load;
		event.source = m_Unit;
		SendEvent(&event);

	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::Load(void)
	{
		init();		
		AIInterface::Load();	
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::Unload(void)
	{
		AIInterface::Unload();
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::Update(const uint32 & p_time)
	{
		UpdateEffect(p_time);
		AIInterface::Update(p_time);
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::UpdateEffect(const uint32 & p_time)
	{
		uint32 time_now = time(0);
		uint32 now_t = now();
		
		Effect* Unit = ((Effect*)m_Unit);

		if(m_effectStatus == EffectStatus_None)
		{
			ResourceEvent event;
			event.event_id = EffectEvent_Active;
			SendEvent(&event);
			m_effectStatus = EffectStatus_Active;
		}
		
		if(Unit->getEffectStatus() != Effect::EffectStatusProcess)
			return;
		
		ResourceEvent event;
		event.event_id = ResourceEvent_Update;
		ActiveEffect(&event, p_time);	
		m_exp_interval = now_t - Unit->common.expend_time;
		m_exp_duration_time = now_t - m_start_time;
		
		if(m_interval_time)
		{
			if(m_exp_interval >= m_interval_time)
			{
				event.event_id = EffectEvent_Interval;
				ActiveEffect(&event, m_exp_interval);
				Unit->common.expend_time = now_t;
				m_exp_interval = 0;			
			}
		}
		
		if(m_congeal_time)
		{
			m_congeal_last_time = now_t - m_congeal_start_time;
			if(m_congeal_last_time >= m_congeal_time)
			{
				m_congeal_time = 0;
				m_congeal_last_time = 0;
				m_congeal_start_time = 0;
			}
		}
		
		if(	Unit->common.start_time && 	Unit->common.end_time)
		{
			if(time_now < Unit->common.start_time)
				return ;
			
			Unit->common.start_time = time_now;
			if( (m_exp_duration_time >= m_leave_duration_time))
			{				
				ResourceEvent event;
				event.event_id = ResouceEvent_TimeOver;				
				ActiveEffect(&event, Unit->common.expend_time);
				sResourceControlMgr.destroyResource(m_Unit);
				
				// if(m_triggerMax)
					// m_triggerMax --;
					
				// m_Unit->setUInt32Field("loop", m_triggerMax);
				// m_Unit->SaveDB();
				// if(!m_triggerMax)
				// {
					// Unit->common.end_time = 0;
				// }
				
				return ;
			}
		}
	}
	
	//-----------------------------------------------------------------------
	uint8 AIEffectInterface::HandleEvent(ResourceEvent * event)
	{
		if (m_Unit->getUInt32Field("effect_type") == en_EffectStatus_Sucess)
			return EVERET_NONE;
	
		return ActiveEffect(event);
	}
	
	//-----------------------------------------------------------------------
	uint8 AIEffectInterface::ActiveEffect(ResourceEvent * event, const uint32 & exp_time)
	{
		Effect* Unit = ((Effect*)m_Unit);
		if(Unit->getEffectStatus() == Effect::EffectStatusProcess && !IsCongeal())
		{						
			EffectAIScript * ai = TO_EFFECT_AI(m_Unit->getAIScript());
			if(!ai)
				return EVERET_NONE;
			
			return ai->OnActiveEffect(event, exp_time);
		}
		
		return EVERET_NONE;
	}	
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::StartCongeal(const uint32 & congeal_timer)
	{
		m_congeal_time = congeal_timer;
		m_congeal_start_time = getCurrentUpdateTime();
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::UpdateLeaveDurationTimer(void)
	{
		Effect* Unit = (Effect*)m_Unit;
		int32 leave = int32(Unit->common.end_time - time(0));
		if(leave > 0)
			m_leave_duration_time = uint32(leave * 1000);	
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::SetEndTimer(const int32 & time)
	{
		if(time == 0)
			return ;
		
		int32 re = int32(m_leave_duration_time) + time;
		if(re <= 0)
			m_leave_duration_time = 0;
		else
			m_leave_duration_time = (uint32)re;
		
		Effect* unit = (Effect*)m_Unit;
		int32 endre = int32(unit->common.end_time) + time;
		if(endre < 0)
			m_Unit->setUInt32Field("end_time", 0);
		else
			m_Unit->setUInt32Field("end_time", endre);
		
		m_Unit->SaveDB();
	}
	
	//-----------------------------------------------------------------------
	void AIEffectInterface::ResetEffectLoop()
	{
		Effect* unit = (Effect*)m_Unit;
		m_exp_duration_time = 0;
		uint32 now_t = now();
		uint32 dur = unit->common.end_time - unit->common.start_time;
		unit->common.start_time = now_t;
		unit->common.end_time = now_t + dur;
		unit->setUInt32Field("start_time", unit->common.start_time);
		unit->setUInt32Field("end_time", unit->common.end_time);
		
		int32 leave = int32(unit->common.end_time - now_t);
		if(leave > 0)
			m_leave_duration_time = uint32(leave * 1000);
		
		m_start_time = now();
		
	}
	
	/******************************任务基类*******************************/		
	AIMissionInterface::AIMissionInterface(Resource * pUnit):AIInterface(pUnit)
	{
		m_AIType = AITYPE_MISSION;	
	}
	
	AIMissionInterface::~AIMissionInterface(void)
	{
	}
	
	//-----------------------------------------------------------------------
	void 	AIMissionInterface::Load(void)
	{
		Mission* pMission = (Mission*)m_Unit;
		uint8 owner_type= m_Unit->getUInt32Field("owner_type");
		uint32 owner_id	= m_Unit->getUInt32Field("owner_id");
		m_ownerProxy._handle 	= owner_id;
		m_ownerProxy._type = ResourceProxy::getResourceType((ResourceTargetType)owner_type);
		
		
		uint8 src_type 	= m_Unit->getUInt32Field("source_type");
		uint32 src_id	= m_Unit->getUInt32Field("source_id");
		m_sourceProxy._handle = src_id;
		m_sourceProxy._type = ResourceProxy::getResourceType((ResourceTargetType)src_type);
		
		uint8 tag_type 	= m_Unit->getUInt32Field("target_type");
		uint32 tag_id 	= m_Unit->getUInt32Field("target_id");
		m_targetProxy._handle = tag_id;
		m_targetProxy._type =ResourceProxy::getResourceType((ResourceTargetType)tag_type);
		
		pMission->common.data1 = pMission->getInt32Field("data1");
		pMission->common.data2 = pMission->getInt32Field("data2");
		pMission->common.data3 = pMission->getInt32Field("data3");
		pMission->common.data4 = pMission->getInt32Field("data4");
		pMission->common.fdata1= pMission->getFloatField("fdata1");
		pMission->common.fdata2= pMission->getFloatField("fdata2");
		
		AIInterface::Load();
	}
	
	//-----------------------------------------------------------------------
	uint8	AIMissionInterface::HandleEvent(ResourceEvent * event)
	{
		return AIInterface::HandleEvent(event);
	}

}
